/*
 * ============================================================================
 * Licensed Materials - Property of IBM
 * Project  Zero
 *
 * (C) Copyright IBM Corp. 2007  All Rights Reserved.
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 * ============================================================================
 * Copyright (c) 1999 - 2006 The PHP Group. All rights reserved.
 * ============================================================================
 */
package com.ibm.p8.engine.parser.custom;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.ibm.p8.engine.parser.core.Parser;
import com.ibm.p8.engine.parser.model.Ast;

// ////////////////////////////////////////////////////////////////
// //
// This file was auto-generated by the LPG Eclipse Tooling. //
// It is safe to edit this file. It will not be overwritten. //
// //
// ////////////////////////////////////////////////////////////////

public class Factory {

	public static final int PARSER = 0;
	public static final int SCANNER = 1;
	public static final int ASSISTANT = 2;
	public static final int AST = 3;

	static final String classNames[] = {
			"com.ibm.p8.engine.parser.custom.CustomParser", // Custom classes
															// can live in this
															// package
			"com.ibm.p8.engine.parser.custom.PHPScanner", // They could be
															// called anything
			"com.ibm.p8.engine.parser.editor.CustomAssistant", // They can live
																// in another
																// package
			"com.ibm.p8.engine.parser.model.Ast", };

	static Map<String, Class> AstClasses = Collections
			.synchronizedMap(new HashMap<String, Class>());
	static Ast skip = new Ast() {
	};
	static final Class skipClass = skip.getClass();
	static final Class classes[];

	static {
		classes = new Class[classNames.length];
	}

	public static Ast createAst(String name) {
		return getCustomAstClassInstance("com.ibm.p8.engine.ast.Ast", name);
	}

	public static Ast createAstTerminal(String name) {
		return getCustomAstClassInstance("com.ibm.p8.engine.ast.AstTerminal_",
				name);
	}

	private static Ast getCustomAstClassInstance(String prefix, String name) {
		try {
			Class c = AstClasses.get(name);
			if (c != skipClass) {
				if (c == null) {
					String className = prefix + name;
					c = Class.forName(className);
					AstClasses.put(name, c);
				}
				return (Ast) c.newInstance();
			}
		} catch (Exception e) {
			AstClasses.put(name, skipClass);
			e.printStackTrace();
		}
		return (Ast) create(AST);
	}

	public static Object create(int id) {
		try {
			Class c = classes[id];
			if (c == null) {
				c = Class.forName(classNames[id]);
				classes[id] = c;
			}
			return c.newInstance();
		} catch (Exception e) {
			if (Parser.getDebug()) {
				e.printStackTrace();
			}
			return null;
		}
	}

}
